<template>
	<div class="" v-show="show">
		<div>
			<div class="mask"></div>
			<div class="chasing-dia-content dis-flex flex-column">
				<div class="chasing-title t-c ft-28 p-r">
					<span>距{{number}}期截止还有</span> <span class="co-red ml-10">{{time}}</span>
					<div class="close-btn" @click="close"><img src="@/assets/close.png" /></div>
				</div>
				<div class="chasing-title1 ft-28 dis-flex">
					<div class="flex-1">追{{list.length}}期</div>
					<div class="chasing-change co-blue" @click="openChange()">修改方案</div>
				</div>
				<div class="chasing-content flex-1">
					<div class="chasing-list-head">
						<div class="dis-flex">
							<div class="th1">期号</div>
							<div class="th2">倍数</div>
							<div class="th3">投入(元)</div>
							<div class="th3">盈利(元)</div>
							<div class="flex-1">盈利率</div>
						</div>
					</div>
					<div class="chasing-list-content t-c ft-24">
						<div class="dis-flex content-item" v-for="(item,index) in list">
							<div class="th1">{{getNumber(index) }}</div>
							<div class="th2"><Inputnumber @change="changebl($event,index)" :min="1" class="ver-mid" v-model="item.bl" /></div>
							<div class="th3">{{parseFloat(item.total).toFixed(2)}}</div>
							<div class="th3 co-red">{{parseFloat(item.maxPrice).toFixed(2)}}</div>
							<div class="flex-1 co-red">{{parseInt(item.maxPrice*100/item.total)}}%</div>
						</div>
					</div>
				</div>
				<div class="content-bottom">
					<div class="dis-flex">
						<div class="ft-24"><Checkbox label="中奖后停止追号" v-model="checked1" /></div>
						<div class="ft-28 flex-1 t-r">共{{list.length}}期 <span class="ft-40 co-red">{{total}}</span> 元</div>
					</div>
					<div class="t-c mt-36">
						<a class="btn btn-red btn-circle btn-submit">立即追号</a>
					</div>
				</div>
			</div>
		</div>

		<Modal v-model="resultShow" @okCall="savechasing" zIndex="1001" title="修改追号方案" cancelBtn="取消" okBtn="生成方案">
			<div class="change-content t-c ft-40">
				<div>
					<span class="ft-28 mr-30">连续追号</span>
					<Inputnumber :min="1" class="w-266" v-model="chasingNumber" />
				</div>
				<div class="mt-20">
					<span class="ft-28 mr-30">起始倍数</span>
					<Inputnumber :min="1" class="w-266" v-model="bei"/>
				</div>
				<div class="radio1 ft-28" >
					<Radio name="radio1" v-model="radioValue" class="radio-1" label="全程盈利率不低于" />
					<input type="text" v-model="yll" class="small-input" />
					<span>%</span>
				</div>
				<div class="radio2 ft-28" >
					<Radio name="radio2"  v-model="radioValue" class="radio-1" label="全程盈利不低于" />
					<input type="text" v-model="ylmoney" class="small-input" />
					<span>元</span>
				</div>
			</div>
		</Modal>
	</div>
</template>
<script>
	import Modal from '@/components/modal';
	import Inputnumber from '@/components/inputNumber';
	import Radio from '@/components/radio';
	import Checkbox from '@/components/checkbox';
	export default {
		props:{
			chooseContent:{},
			number:{
				type:String,
				default:""
			},
			time:{
				type:String,
				default:""
			},
			show:Boolean,
			value:{
				type:Array,
				default:[]
			}
		},
		computed:{
			total(){
				let len=this.list.length;
				return len>0?this.list[len-1].total||0:0;
			}
		},
		watch:{
			value(newvalue){
				this.resetlist(newvalue);

			}
		},
		data(){
			return {
				list:[],
				ylmoney:"",
				yll:"",
				radioValue:"",
				chasingNumber:10,
				bei:2,
				resultShow:false,
				checked1:false
			}
		},
		components:{
			Modal,
			Inputnumber,
			Radio,
			Checkbox
		},
		methods:{
			resetlist(newvalue){
				let total=0;
				let money=this.chooseContent.money;

				let maxPrice=this.chooseContent.maxPrice;
				newvalue.forEach(item=>{
					item.total=item.bl*money+total;
					item.maxPrice=item.bl*maxPrice-item.total;

					total+=item.bl*money;
				});
				this.list=newvalue;
			},
			changebl(value,index){//修改倍率
				let list=this.list;
				let len=list.length;
				let total=0;
				let money=this.chooseContent.money;
				let maxPrice=this.chooseContent.maxPrice;
				if (index>0) {
					total=list[index-1].total; 
				}
				for(let i=index;i<len;i++){
					let item=list[i];
					let nowValue=i==index?value:item.bl;
					item.total=nowValue*money+total;
					item.maxPrice=nowValue*maxPrice-item.total;

					total+=nowValue*money;
					console.log("maxPrice:"+item.maxPrice);
					console.log("total1:"+total);
				}
				this.list=list;
			},
			get2num(price1,price2){
				let i=0;
				let price=Math.ceil(price1/price2);
				while(parseInt(price/2)>0){
					price=price/2;
					i++;
				}
				return i+1;
			},
			getnum(total,ylmoney,chooseContent){
				return Math.ceil((ylmoney+total)/((chooseContent.maxPrice-chooseContent.money) ) );
			},
			getnuml(total,ylbl,chooseContent){
				return Math.ceil((total+ylbl*total)/((chooseContent.maxPrice-chooseContent.money)-ylbl*chooseContent.money));


			},
			savechasing(){
				let len=this.chasingNumber;
				let bl=this.bei;
				let ylmoney=parseInt(this.ylmoney);
				let yll=parseInt(this.yll);
				let chooseContent=this.chooseContent;
				let radioValue=this.radioValue;//选项选择
				let arr=[];
				if (radioValue=="radio1") {
					let total=0;
					for(let i=0;i<=len;i++){
						let bl=1;
						if (i>0) {
							bl=this.getnuml(total,parseInt(yll)/100,chooseContent);
							if (bl<=0) {
								break;
							}
						}else{
							let yll1=(chooseContent.maxPrice-chooseContent.money)/chooseContent.money;
							if (yll1<yll/100) {
								break;//没有符合条件
							}
						}
						
						total+=bl*chooseContent.money;
						arr.push({bl:bl});
					}
				}else if(radioValue=="radio2"&&ylmoney){
					let total=0;
					for(let i=0;i<=len;i++){
						let bl=this.getnum(total,ylmoney,chooseContent,i);
						total+=bl*chooseContent.money;

						arr.push({bl:bl,total,maxPrice:bl*chooseContent.maxPrice-total});
					}
				}else{
					for(let i=0;i<len;i++){
						arr.push({bl:Math.pow(2,i)});
					}
				}
				this.resetlist(arr);
				//this.list=arr;
				//this.$emit('input',arr);
				this.resultShow=false;
			},
			getNumber(index){
				let number=this.number;
				let nowNumber=index+parseInt(number.substring(number.length-3,number.length))
				return nowNumber<100?"0"+nowNumber:nowNumber;
			},
			close(){
				this.show=false;
				this.$emit('close');
			},
			openChange(){
				this.resultShow=true;
			}
		}
	}
</script>
<style scoped lang="scss">
	@import '@/css/base.scss';
	.chasing-dia-content{position: fixed;left:0 ;bottom: 0;width: 100%;height: 95%;background: #FFF;z-index: 1001;box-shadow: 0 28px 40px 0 rgba(199,199,199,0.25);border-top-left-radius: 16px;border-top-right-radius: 16px;min-height: 90%;}
	.chasing-title{height: 100px;line-height: 100px;border-bottom: 1px solid transparent; border-image: svg(1px-border param(--color #F5F5F5)) 2 2 stretch;border-top:0;}
	.chasing-title1{padding: 24px 40px;}
	.chasing-list-head{padding: 20px 0;background: #F8F8F8;}
	.chasing-list-head>div{height: 32px;line-height: 32px;font-size: 22px;text-align: center;}

	.chasing-content{overflow: auto;-webkit-overflow-scrolling: touch;}
	.chasing-list-head{
		 .th1,.th2,.th3{border-right: 1px solid #D9D9D9;}
	}
	.th1{width: 119px;}
	.th2{width: 192px;}
	.th3{width: 140px;}
	.w-266{width: 266px!important;}
	.content-item{height: 100px;line-height: 100px;}
	.content-item:nth-child(2n){
		background: $gray-f5;
	}

	.content-bottom{height: 236px;box-sizing:border-box;padding: 30px 40px;box-shadow: 0 -2px 0 0 rgba(213,213,213,0.47);}

	.close-btn{position: absolute;right: 10px;top:10px;width: 72px;height: 72px;text-align: 
		center;line-height: 72px;}
	.close-btn img{width: 36px;height: 36px;display: inline;vertical-align: middle;}

	.small-input{width: 80px;margin-left: 32px;}
	.radio1{margin-top: 72px;}
	.radio2{margin-top: 20px;}
	.radio-1{width: 254px;text-align: left;}
</style>
